extern uniform matrix gWorldViewProj;
extern uniform matrix gWorldInvTrans;
extern uniform matrix gWorld;

extern uniform float4 gAmbientLight;
extern uniform float4 gDiffuseLight;
extern uniform float4 gSpecularLight;
extern uniform float4 gAmbientMtrl;
extern uniform float4 gDiffuseMtrl;
extern uniform float4 gSpecularMtrl;
extern uniform float  gSpecularPower;

extern uniform float3 gEyePosition;
extern uniform float3 gLightPosition;
extern uniform float3 gLightDir;
extern uniform float  gSpotPower;

extern uniform texture gTexture0;

sampler tex0 = sampler_state
{
	Texture = <gTexture0>;
	MinFilter = Linear;
	MagFilter = Linear;
	MipFilter = Linear;
};

struct VS_INPUT
{
	float3 position : POSITION0;
	float3 normal : NORMAL0;
	float2 texCoord : TEXCOORD0;
};

struct VS_OUTPUT
{
	float4 position : POSITION0;
	float3 positionW : TEXCOORD0;
	float3 normalW : TEXCOORD1;
	float2 texCoord : TEXCOORD2;
};

VS_OUTPUT PointLight_VS(VS_INPUT input)
{
	VS_OUTPUT output = (VS_OUTPUT)0;
	output.position = mul(float4(input.position, 1.0f), gWorldViewProj);
	output.positionW = mul(float4(input.position, 1.0f), gWorld).xyz;
	output.normalW = mul(float4(input.normal, 0.0f), gWorldInvTrans).xyz;
	output.texCoord = input.texCoord;
	return output;
}

float4 PointLight_PS(float3 positionW : TEXCOORD0, float3 normalW : TEXCOORD1, float2 texCoord : TEXCOORD2) : COLOR
{
	normalW = normalize(normalW);
	float3 toLightDir = normalize(gLightPosition - positionW);
	float3 reflectLightDir = reflect(-toLightDir, normalW);
	float3 toEyeDir = normalize(gEyePosition - positionW);

	float4 ambient = gAmbientLight * gAmbientMtrl;

	float d = max(dot(toLightDir, normalW), 0);
	float4 diffuse = d * gDiffuseLight * gDiffuseMtrl;

	float s = pow(max(dot(reflectLightDir, toEyeDir), 0), gSpecularPower);
	float4 specular = s * gSpecularLight * gSpecularMtrl;

	float4 color = ambient + diffuse + specular;

	color = color * tex2D(tex0, texCoord);

	color.a = gDiffuseMtrl.a;

	return color;
}

technique DirLightTech
{
	pass P0
	{
		VertexShader = compile vs_2_0 PointLight_VS();
		PixelShader = compile ps_2_0 PointLight_PS();

		AlphaBlendEnable = true;
		SrcBlend = SrcAlpha;
		DestBlend = InvSrcAlpha;
	}
}